//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>

#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE 5000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool SplitColumn(char* buffer, TListE<String>* columns, const char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    String* cell = new String(part);
    cell->Trim();
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main(int argc, char** argv) {

  String* filenameIn;
  String* filenameOut;
  String* filenameError;

  if (argc < 4) {
    cout << "Error in the input arguments. The execution must be: " << endl;
    cout << "./FilterProtonASmallLigandInSMI <Input SMI file> <output SMI file> <Error file>" << endl;
    return 0;
  }
  else {
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << " Input SMI File : " << argv[1] << endl;
    cout << " Ouput SMI File : " << argv[2] << endl;
    cout << "     Error File : " << argv[3] << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    filenameIn    = new String(argv[1]);
    filenameOut   = new String(argv[2]);
    filenameError = new String(argv[3]);
  }

  fstream fileIn, fileOut, fileError;

  fileIn.open(filenameIn->Out(),   fstream::in);
  fileOut.open(filenameOut->Out(), fstream::out);
  fileError.open(filenameError->Out(), fstream::out);

  char             buffer[LENGTHLINE + 1];
  TListE <String>* list    = new TListE <String> ();
  TListE <String>* ligands = new TListE <String> ();

  while (!fileIn.eof()) {

    fileIn.getline(buffer, LENGTHLINE);
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << "Analizing: " << buffer << endl;
    list->Clear();
    SplitColumn(buffer, list, " \n\r");

    if (list->Length()) {
      ligands->Clear();
      SplitColumn(list->Get(0)->Out(), ligands, ".");
      if (ligands->Length()) {
        int position  = 0;
        int lengthRef = ligands->Get(0)->Length();
        for (int i = 0; i < ligands->Length(); i++)
          if (ligands->Get(i)->Length() > lengthRef) {
            lengthRef = ligands->Get(i)->Length();
            position  = i;
          }

        if (ligands->Get(position)->Length() >= 10) {
          fileOut << ligands->Get(position)->Out() << " " << list->Get(1)->Out() << endl;
          cout    << "Result: " << ligands->Get(position)->Out() << " " << list->Get(1)->Out() << endl;
        }
        else {
          fileError << ligands->Get(position)->Out() << " " << list->Get(1)->Out() << endl;
          cout      << "Error: " << ligands->Get(position)->Out() << " " << list->Get(1)->Out() << endl;
        }

      }
      else {
        fileOut << "Result: " << buffer;
        cout    << "Result: " << buffer;
      }
    }
    else {
      fileOut << "Result: " << buffer;
      cout    << "Result: " << buffer;
    }

  }

  fileIn.close();
  fileOut.close();
  fileError.close();

  if (list)          delete list;
  if (ligands)       delete ligands;

  if (filenameIn)    delete filenameIn;
  if (filenameOut)   delete filenameOut;
  if (filenameError) delete filenameError;

  return 1;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//




